$(function () {
  let pathInput = $(".input-row > input")[0];
  let jsonTextarea = $(".text-row > textarea")[0];
  let jsonVierwer = $(".right").jsonViewer();

  let localPath = localStorage.getItem("path");
  if (localPath) pathInput.value = localPath;

  $(".input-row > .parse").click(function () {
    let path = pathInput.value;
    let textareaStr = jsonTextarea.value;

    if (!textareaStr || textareaStr.length < 1) return;

    localStorage.setItem("path", path);

    let object = JSON.parse(textareaStr);

    let resultObject = _.get(object, path);

    if (_.isString(resultObject)) resultObject = JSON.parse(resultObject);

    $(".right").jsonViewer(resultObject);
  });

  $(".input-row > .clean").click(function () {
    jsonTextarea.value = "";
  });
});
